package cn.jimu98.template;

import cn.jimu98.common.templates.DirectiveHandler;
import cn.jimu98.common.templates.TemplateDirective;
import cn.jimu98.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Classname RankTemplate
 * @Description TODO
 * @Date 2020/6/10 14:17
 * @Created by kaibo
 */
@Component
public class HotsTemplate extends TemplateDirective {
    @Autowired
    RedisUtil redisUtil;
    @Override
    public String getName() {
        return null;
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {

        String weekRankKey ="week:rank";
        Set<ZSetOperations.TypedTuple> typedTuples = redisUtil.getZSetRank(weekRankKey, 0, 6);
        List<Map> hotPosts = new ArrayList<>();
        for (ZSetOperations.TypedTuple typedTuple : typedTuples) {
            System.out.println(typedTuple);
            Map<String,Object> map =new HashMap<>();
            Object value = typedTuple.getValue();
            String postKey ="rank:post:"+value;
            map.put("id", value);
            map.put("title",redisUtil.hget(postKey,"post:title"));
            map.put("commentCount",typedTuple.getScore());
            hotPosts.add(map);

        }
        handler.put(RESULTS,hotPosts).render();
    }
}
